﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Server.Interface;

namespace MaxStudio.Concrete.Server.Service
{
    public class FunctionGroupService : ServiceHandle, IFunctionGroup
    {
        #region IFunctionGroup Members
     //   ConcreteDaoDataContext dataContext = new ConcreteDaoDataContext();

        public OperationResult<List<tb_FunctionGroup>> getAllFunctionList(int currentPage, int PageSize, out int totalPage)
        {
              totalPage = 0;

            return getAllFunctionList(getDateContext());
        }

        public OperationResult<List<tb_FunctionGroup>> getAllFunctionList(ConcreteDaoDataContext dataContext)
        {
            OperationResult<List<tb_FunctionGroup>> result = new OperationResult<List<tb_FunctionGroup>>();
            List<tb_FunctionGroup> list = new List<tb_FunctionGroup>();
            try
            {
                list = dataContext.getAllFunctionGroup();
                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }      

        public OperationResult<tb_FunctionGroup> craeteFunctionGroup( tb_FunctionGroup instance)
        {
            OperationResult<tb_FunctionGroup> result = new OperationResult<tb_FunctionGroup>();

            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.createFunctionGroup(instance);
                dataContext.SubmitChanges();
                result.obj = instance;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_FunctionGroup> modifyFunctionGroup(tb_FunctionGroup instance)
        {
            OperationResult<tb_FunctionGroup> result = new OperationResult<tb_FunctionGroup>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {

                dataContext.modifyFunctionGroup(instance.GroupName, instance.Description, instance.ModifyBy, DateTime.Now, instance.IsDelete, instance.Id);
                dataContext.SubmitChanges();
                result.obj = instance;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_FunctionGroup> deleteFunctionGroup(tb_FunctionGroup instance)
        {
            OperationResult<tb_FunctionGroup> result = new OperationResult<tb_FunctionGroup>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.deleteFunctionGroup(instance);
                dataContext.SubmitChanges();
                result.obj = instance;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<List<tb_FunctionGroup>> getFunctionGroupById(int id)
        {
            OperationResult<List<tb_FunctionGroup>> result = new OperationResult<List<tb_FunctionGroup>>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                List<tb_FunctionGroup> list = new List<tb_FunctionGroup>();
                tb_FunctionGroup functionGroup = dataContext.getFunctionGroupByID(id);
                list.Add(functionGroup);
                if (functionGroup == null)
                {
                    result.errorMessageKey = "No exits";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }
            return result;
        }

        #endregion

        #region IFunctionGroupMapping Members 

        public OperationResult<List<tb_Mapping_FunctionGroup>> getFunctionGroupMappingByGroupId(int GroupId)
        { 
            ConcreteDaoDataContext  dataContext =getDateContext();
            OperationResult<List<tb_Mapping_FunctionGroup>> result = new OperationResult<List<tb_Mapping_FunctionGroup>>();
            List<tb_Mapping_FunctionGroup> list = new List<tb_Mapping_FunctionGroup>();
            try
            {
                list = dataContext.getFunctionGroupMappingByGroupId(GroupId);
                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_Mapping_FunctionGroup> createFunctionGroupMapping(tb_Mapping_FunctionGroup instance)
        {
            OperationResult<tb_Mapping_FunctionGroup> result = new OperationResult<tb_Mapping_FunctionGroup>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.createFunctionGroupMapping(instance);
                dataContext.SubmitChanges();
                result.obj = instance;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_Mapping_FunctionGroup> deleteFunctionGroupMapping(int GroupId)
        {
            OperationResult<tb_Mapping_FunctionGroup> result = new OperationResult<tb_Mapping_FunctionGroup>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.deleteFunctionGroupMapping(GroupId);
                dataContext.SubmitChanges();
               // result.obj = instance;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        #endregion
    }
}
